package com.kharthick.pwGame.units;

import java.util.Vector;
import java.awt.Graphics;
import java.awt.MediaTracker;

import com.kharthick.game.Core;
import com.kharthick.pwGame.sprites.Sprite;
import com.kharthick.pwGame.sprites.SpriteFactory;

public abstract class AbstractUnit implements Unit {
	protected String name;		// name of unit (unused)
	protected String type;
	private boolean active;		// is unit active?
	private boolean moveActive;
	private boolean attackActive;
	
	protected int health;
	protected int attack;
	protected int defense;
	protected int range;
	protected int movement;
	
	private Sprite sprite;
	private Sprite idle;
	private Sprite walkLeft, walkRight, walkDown, walkUp;
	private Sprite attackLeft, attackRight, attackDown, attackUp;
	private final Sprite deathSprite;
	
	private Vector<Integer> position;
	
	private int displaceX, displaceY;
	private int count;
	private int nextTileX, nextTileY;
	private int directionX, directionY;
	private int closeX, closeY;
	private int tX, tY;
	
	AbstractUnit(String idle, String left, String right, 
			String down, String up, String la, String ra,
			String da, String ua, Vector<Integer> pos, MediaTracker tracker) {
		
		//The sprite factory is too smart for its own good
		//Since I try to save loading time by passing two references of the save picture
		//Just cutting on a little differently, the factory recognizes its the same picture
		// and doesn't create another one.... so the if else statement doesn't work yet.
		if(idle == null)
			this.idle = SpriteFactory.getSprite(down, 1, 50, 50, tracker);
		else
			this.idle = SpriteFactory.getSprite(idle, tracker);
		
		walkLeft = SpriteFactory.getSprite(left, 5, 50, 50, tracker);
		walkRight = SpriteFactory.getSprite(right, 5, 50, 50, tracker);
		walkDown = SpriteFactory.getSprite(down, 5, 50, 50, tracker);
		walkUp = SpriteFactory.getSprite(up, 5, 50, 50, tracker);
		
		attackLeft = SpriteFactory.getSprite(la, 2, 50, 50, tracker);
		attackRight = SpriteFactory.getSprite(ra, 2, 50, 50, tracker);
		attackDown = SpriteFactory.getSprite(da, 2, 50, 50, tracker);
		attackUp = SpriteFactory.getSprite(ua, 2, 50, 50, tracker);
		
		sprite = this.idle;
		deathSprite = SpriteFactory.getSprite("dead.png", tracker);
		
		position = pos;	
	}
	
	public void activate() {
		// Turn on the unit
		active = true;
	}

	public void deactivate() {
		// Turn off the unit
		active = false;
	}
	
	public boolean isActive(){
		return active;
	}
	
	public void activateAttack(int targetX, int targetY){
		count = 1;
		closeY = position.get(1);
		closeX = position.get(0);
		tX = targetX;
		tY = targetY;
		attackActive = true;
	}
	
	public void activateMove(int targetX, int targetY){
		System.out.println("moveactivated");
		count = 1;
		closeY = position.get(1);
		closeX = position.get(0);
		tX = targetX;
		tY = targetY;
		displaceX = 0;
		displaceY = 0;
		//System.out.println(closeX + " " + closeY + " " + tX + " " + tY);
		moveActive = true;
	}
	
	public void attackAnimation(){
		directionX = getDirectionX();
		directionY = getDirectionY();
		if(directionX  > 0){
			//System.out.println("Attackright");
			sprite = attackRight;
		}
		else if(directionX  < 0){
			//System.out.println("Attackleft");
			sprite = attackLeft;
		}
		else if(directionY  > 0){
			//System.out.println("Attackdown");
			sprite = attackDown;
		}
		else if(directionY  < 0){
			//System.out.println("Attackup");
			sprite = attackUp;
		}
		//buffer.drawString("Damage: " + damage, aX*blockSize, aY*blockSize-20);
		//buffer.drawString("Hp: " + nHp, hX*blockSize, hY*blockSize);
		if(count == 4){
			count = 1;
			sprite = idle;
			attackActive = false;
			//System.out.println("SHOULD BE OFF!");
			//buffer.drawString("Damage: " + damage, aX*blockSize, aY*blockSize-20);
			//buffer.drawString("Hp: " + nHp, hX*blockSize, hY*blockSize);
		}
		else
			count ++;
	}
	
	public void moveAnimation(){
		int scrollX = 0, scrollY = 0;
		directionX = getDirectionX();
		directionY = getDirectionY();
		nextTileX = ((closeX-position.get(0))*Core.TILE_SIZE);
		nextTileY = ((closeY-position.get(1))*Core.TILE_SIZE);
		
		if(directionX  > 0){	
			
			if(closeX*Core.TILE_SIZE + 10*count < tX*Core.TILE_SIZE+10){
				sprite = walkRight;
				displaceX = 10*(count)+nextTileX;
				//System.out.println(displaceX);
			}
			else{
				//System.out.println("it happens");
				sprite = walkDown;
				displaceX = 10*(count-1)+nextTileX;
			}
			if(count == 6){
				closeX++;
				count = 1;
			}
			else
				count++;
		}
		else if(directionX  < 0){
			sprite = walkLeft;
			displaceX = -(10*count) + nextTileX;
			if(count == 6){
				closeX--;
				count = 1;
			}
			else
				count++;
			
		}
		else if(directionY  > 0){
			if(closeY*Core.TILE_SIZE + 10*count < tY*Core.TILE_SIZE){
				sprite = walkDown;
				displaceY = 10*count + nextTileY;
			}
			else{
				sprite = walkDown;
				displaceY = 10*(count-1) + nextTileY;
			}
			if(count == 6){
				closeY++;
				count = 1;
			}
			else
				count++;
		}
		else if(directionY  < 0){
			sprite = walkUp;
			displaceY = -(10*count) + nextTileY;
			if(count == 6){
				closeY--;
				count = 1;
			}
			else
				count++;			
		}
		else{
			sprite = idle;
			moveActive = false;
			setPosition(tX,tY);
			displaceX = 0;
			displaceY = 0;
		}
		//System.out.println(displaceX + " " + displaceY );
	}

	private int getDirectionY() {
		return tY-closeY;
	}

	private int getDirectionX() {
		return tX-closeX;
	}

	public void draw(Graphics buf) {
		// Draw the unit
		if(moveActive == true){
			moveAnimation();
		}
		else if(attackActive == true){
			attackAnimation();
		}
		buf.drawImage(sprite.getImage(),	// the actual tile
				Core.TILE_SIZE*position.get(0) + displaceX,	// x position
				Core.TILE_SIZE*position.get(1) + displaceY,  // y position
				null);
	}
	
	public Vector<Integer> getPosition(){
		return position;		
	}
	
	public void setPosition(int row, int col){
		position.set(0, new Integer(row));
		position.set(1, new Integer(col));
	}
	
	public void setHp(int Hp){
		if(Hp > 0)
			health = Hp;
		else
			sprite = deathSprite;
	}
	
	public int getRange(){
		return range;
	}
	
	public int getMovement(){
		return movement;
	}
	
	public int getAttack(){
		return attack;
	}
	
	public int getHp(){
		return health;
	}
	
	public String getType(){
		return type;
	}
	
	public void leftClick(){
		System.out.println("clicked on unit");
		if(active = true) deactivate();
		else activate();
	}
	
}
